package main

import "fmt"

func main() {
	s := "abcabcbb"
	s = "tmmzuxt"
	v := lengthOfLongestSubstring(s)
	fmt.Println(v)
}

// 无重复字符的最长子串
// https://leetcode.cn/problems/longest-substring-without-repeating-characters/
func lengthOfLongestSubstring(s string) int {
	l, r := 0, 0
	n := len(s)
	sByte := []byte(s)

	resLen := 0
	chatMap := map[byte]int{} //记录字符所在位置
	for r < n {
		c := sByte[r]
		r++
		if p, ok := chatMap[c]; !ok || l > p {
			chatMap[c] = r - 1
			resLen = max(resLen, r-l)
			continue
		}
		// 有重复的
		l = chatMap[c] + 1
		chatMap[c] = r - 1
	}
	return resLen
}

func max(v1, v2 int) int {
	if v1 > v2 {
		return v1
	}
	return v2
}
